package clases;

import java.util.ArrayList;

public class GeneticAlgorithm {
  // tamanio total de la poblacion
  public static final int POP_SIZE = 500;
  // cantidad de individuos que se seleccionan para reproducirse
  public static final int K = (int) Math.ceil(0.2 * POP_SIZE);
  // maxima cantidad de generaciones
  public static final int MAX_GENERATIONS = 50;
  // probabilidad de que mute un individuo
  public static final double P_MUT = 0.0001;
  // metodo de seleccion a utilizar:
  // ELITE, RULETA, UNIVERSAL, ELITE+RULETA, ELITE+UNIVERSAL
  public static final String SELECTION_METHOD = "ELITE";
  // metodo de reemplazo a utilizar:
  // ELITE, RULETA, UNIVERSAL, ELITE+RULETA, ELITE+UNIVERSAL
  public static final String REPLACEMENT_METHOD = "ELITE";

  private static final String[] str = { "10000000100000001000000000000000",
      "01000000010000000100000000000000", "00100000001000000010000000000000",
      "00010000000100000001000000000000", "00001000000010000000100000000000",
      "00000100000001000000010000000000", "00000010000000100000001000000000",
      "00000001000000010000000100000000", "00000000111111110000000010000000",
      "00000000000000000000000001000000", "00000000000000000000000000100000",
      "00000000000000000000000000010000", "00000000000000000000000000001000",
      "00000000000000000000000000000100", "00000000000000000000000000000010",
      "00000000000000000000000000000001"};

  public static void main(String[] args) throws Exception {
    String best = new String("b = [ ");
    String mean = new String("m = [ ");
    String path = new String("/Users/marcospianelli/Desktop/Salida/");
    double acumBest = 0.0, acumMean = 0.0;
    ArrayList<Population> pops = new ArrayList<Population>(16);
    for (int i = 0; i < 16; i++) {
      pops.add(i, new Population(POP_SIZE, GeneticAlgorithm.str[i]));
      pops.get(i).randomPopulation();
    }

    int current_generation = 0;
    while (current_generation < MAX_GENERATIONS) {
      acumBest = 0.0;
      acumMean = 0.0;
      for (int i = 0; i < 16; i++) {
        Population parents = pops.get(i).selection(K, SELECTION_METHOD);
        Population offsprings = parents.reproduction();
        pops.get(i).replace(offsprings, REPLACEMENT_METHOD);
        /*
         * para agregar aleatoriedad en la poblacion if(current_generation % 10 ==
         * 0){ Population nuevos = new
         * Population((int)Math.ceil(POP_SIZE*0.1),str);
         * nuevos.randomPopulation(); pop.mutarPoblacion(nuevos); }
         */
        acumBest += pops.get(i).best.getAptitude();
        acumMean += pops.get(i).meanAptitude;
      }
      current_generation++;
      best = best + acumBest
          + (current_generation == MAX_GENERATIONS ? " ];" : " , ");
      mean = mean + acumMean
          + (current_generation == MAX_GENERATIONS ? " ];" : " , ");
      System.out.println("generation: " + current_generation);// + " | max: "
    }
    System.out.println(best);
    System.out.println(mean);
    
    boolean completed = true;
    
    for (int i = 0; i < 16; i++) {
    	
      if(!pops.get(i).best.getOutput().equals(str[i]))	
    	  completed = false;
    	
      System.out.println("");
      pops.get(i).printBest();
      System.out.println(str[i]);
      pops.get(i).best.generateDOT(path + "_" + i + ".dot");
    }
    
    System.out.println("Paso el test:"+completed);
    
    
  }
}
